package com.yc.web.filter;

import com.google.gson.Gson;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

//@WebFilter("/admin/*")
public class AmdinFilters extends CommonFilter{
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest) request;
        HttpSession session=req.getSession();
        if (session.getAttribute("admin") !=null){
            chain.doFilter(request,response);
        }else {
            Map<String, Object> map=new HashMap<String,Object>();
            map.put("code",0);
            map.put("error","访问后台权限不够或没有登录");
            response.setContentType("text/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            Gson g = new Gson();
            out.println(  g.toJson(  map  )); ///后端 把 运行情况 以json类型传出到前端
            out.flush();
            out.close();
        }
    }
}
